package com.youdianstar.app.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.youdianstar.app.R
import com.youdianstar.app.entity.PromoRecord
import com.youdianstar.app.common.ex.getViewColor
import com.youdianstar.app.common.util.DateUtil


/**
 * File: PromoPagingAdapter
 * Author: 82149 Create: 2022/3/2 15:24
 * Changes (from 2022/3/2)
 * --------------------------------------------------
 * description: 优惠码兑换记录(弃用)
 * ---------------------------------------------------
 *
 */
class PromoPagingAdapter(val mContext: Context) :
    PagingDataAdapter<PromoRecord, PromoPagingAdapter.PromoHolder>(
        differCallback
    ) {

    companion object {
        val TAG = "PromoPagingAdapter"
        val differCallback = object : DiffUtil.ItemCallback<PromoRecord>() {
            override fun areItemsTheSame(oldItem: PromoRecord, newItem: PromoRecord): Boolean {
                return oldItem.id == newItem.id
            }

            override fun areContentsTheSame(oldItem: PromoRecord, newItem: PromoRecord): Boolean {
                return oldItem == newItem
            }
        }
    }

    class PromoHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val container = itemView.findViewById<ConstraintLayout>(R.id.cl_container_record)
        val tvTitle = itemView.findViewById<TextView>(R.id.item_title)
        val tvDate = itemView.findViewById<TextView>(R.id.item_date)
        val tvContent = itemView.findViewById<TextView>(R.id.item_contents)
        val tvCode = itemView.findViewById<TextView>(R.id.item_state)
    }

    override fun onBindViewHolder(holder: PromoHolder, position: Int) {
        getItem(position)?.let {
            holder.tvTitle?.text = it.promoCodeTitle
            holder.tvDate?.text = if (null == it.createTime) "" else DateUtil.getOrderDate(it.createTime)

            var titleColor: Int = 0
            //1-未使用 2-已使用 3-已过期
            when (it.state) {
                1 -> {
                    titleColor = R.color.color_default
                    val contentStr = String.format(
                        mContext.getString(R.string.Public_form_content),
                        "Valid until",
                        (if (it.endTime == null) "" else DateUtil.getOrderDate(it.endTime))
                    )
                    holder.tvContent?.text = contentStr
                }
                2 -> {
                    titleColor = R.color.text_default
                    val contentStr = String.format(
                        mContext.getString(R.string.Public_form_content),
                        mContext.getString(R.string.order_orderID),
                        (it.orderNo ?: "")
                    )
                    holder.tvContent?.text = contentStr
                }
                3 -> {
                    titleColor = R.color.color_999999
                    holder.tvContent?.text = "Expired"
                }
            }
            holder.tvTitle?.setTextColor(getViewColor(mContext, titleColor))
            holder.tvCode?.text = it.promoCode
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PromoHolder {
        val view = LayoutInflater.from(mContext).inflate(R.layout.layout_item_promo_record, parent, false)
        return PromoHolder(view)
    }
}